/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JDialogListaArticulo.java
 *
 * Created on Jun 5, 2010, 10:15:30 AM
 */

package tiendagui.internal;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;
import javax.swing.JTable;

import model.ModelListaArticulo;
import controller.ControllerListaArticulos;
import controller.utils.ManagerTabla;
import entities.vo.ItemTablaVO;

/**
 *
 * @author Jhonny
 */
public class JDialogListaArticulo extends javax.swing.JDialog {
	private static final long serialVersionUID = 4420620197411981855L;
	
	private ManagerTabla managerTabla;
    private ControllerListaArticulos controller;
    /** Creates new form JDialogListaArticulo */
    public JDialogListaArticulo(java.awt.Frame parent, boolean modal,ModelListaArticulo model) {
        super(parent, modal);
        initComponents();
        controller = new ControllerListaArticulos(this, model);
        managerTabla = new ManagerTabla(table);
        managerTabla.setController(controller);
    }

    @Override
    public void setVisible(boolean b) {
        if(b){
            try {
                controller.initComponentView();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(JDialogListaArticulo.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(JDialogListaArticulo.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        super.setVisible(b);
    }


    public JTable getTable() {
        return table;
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {

        panelMain = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        buttonCancelar = new javax.swing.JButton();
        buttonAgregar = new javax.swing.JButton();
        buttonBuscar = new javax.swing.JButton();
        textFieldBuscar = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(tiendagui.TiendaGUIApp.class).getContext().getResourceMap(JDialogListaArticulo.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setModal(true);
        setName("Form"); // NOI18N
        setResizable(false);

        panelMain.setName("panelMain"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Referencia", "Stock", "Descripcion", "Precio", "Oferta"
            }
        ) {
            /**
			 * 
			 */
			private static final long serialVersionUID = -6363346385288482998L;
			Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.Float.class, java.lang.Float.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        table.setName("table"); // NOI18N
        jScrollPane1.setViewportView(table);

        buttonCancelar.setText(resourceMap.getString("buttonCancelar.text")); // NOI18N
        buttonCancelar.setName("buttonCancelar"); // NOI18N
        buttonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelarActionPerformed(evt);
            }
        });

        buttonAgregar.setText(resourceMap.getString("buttonAgregar.text")); // NOI18N
        buttonAgregar.setName("buttonAgregar"); // NOI18N
        buttonAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonAgregarActionPerformed(evt);
            }
        });

        buttonBuscar.setText(resourceMap.getString("buttonBuscar.text")); // NOI18N
        buttonBuscar.setName("buttonBuscar"); // NOI18N
        buttonBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBuscarActionPerformed(evt);
            }
        });

        textFieldBuscar.setText(resourceMap.getString("textFieldBuscar.text")); // NOI18N
        textFieldBuscar.setToolTipText(resourceMap.getString("textFieldBuscar.toolTipText")); // NOI18N
        textFieldBuscar.setName("textFieldBuscar"); // NOI18N

        javax.swing.GroupLayout panelMainLayout = new javax.swing.GroupLayout(panelMain);
        panelMain.setLayout(panelMainLayout);
        panelMainLayout.setHorizontalGroup(
            panelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelMainLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(textFieldBuscar, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonBuscar)
                .addGap(35, 35, 35))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelMainLayout.createSequentialGroup()
                .addContainerGap(252, Short.MAX_VALUE)
                .addComponent(buttonAgregar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonCancelar)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 418, Short.MAX_VALUE)
        );

        panelMainLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buttonAgregar, buttonCancelar});

        panelMainLayout.setVerticalGroup(
            panelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelMainLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(buttonBuscar)
                    .addComponent(textFieldBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(panelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonCancelar)
                    .addComponent(buttonAgregar))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-434)/2, (screenSize.height-384)/2, 434, 384);
    }// </editor-fold>

    private void buttonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelarActionPerformed
        controller.cerrar();
    }//GEN-LAST:event_buttonCancelarActionPerformed

    private void buttonAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAgregarActionPerformed
        ((ControllerListaArticulos)controller).agregarArticulo();
    }//GEN-LAST:event_buttonAgregarActionPerformed
    
    private void buttonBuscarActionPerformed(java.awt.event.ActionEvent evt) {
    	((ControllerListaArticulos)controller).buscar();
    }
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonAgregar;
    private javax.swing.JButton buttonCancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel panelMain;
    private javax.swing.JTable table;
    private javax.swing.JButton buttonBuscar;
    private javax.swing.JTextField textFieldBuscar;
    // End of variables declaration//GEN-END:variables
	public ItemTablaVO getArticulo() {		
		return controller.getArticulo();
	}

	public void setBuscar(String value) {
		textFieldBuscar.setText(value);		
	}
	public Integer getBuscar() {
		Integer out=null;
		try{
			out = Integer.valueOf(textFieldBuscar.getText());
		}catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Solo se puede buscar por referencia.");
		}
		return out;		
	}
}
